var server = require('./server_analyse_car.js');
var udp = require('./udpserver.js');
var server_post = require('./server_post.js');
var define = require("./db_define.js");
var mysql = require('mysql');


var obj = {};

obj.port = 8054; //tcp端口启动为8053

obj.protocol_cs = {
    deviceid: { offset: 1, len: 4 },
    cmd: { offset: 5, len: 1 },
    contentlen: { offset: 6, len: 1, includeht: 0 }, //includeht 0:不包含头部
    bodyfields: [
        {
            cmd: 0x01,
            dir: "cs",
            field: [
            { name: "devicestatus", des: "设备状态字", type: "int8", ftype: "", len: 1 },
            {
                name: "deviceerror", des: "设备故障字", type: "int8", ftype: "", len: 1,
                alert: [{ level: 1, low:1,high:20},
                    { level: 2, low:31,high:50 },
                    {level:3,low:61,high:63}
                ]
                //1-20 第一层权限 暂定6个报警，1-6
//31-50 第二层权限，暂定3个报警 31-33
//61-80 第三层权限，暂定3 个报警 61-63

            },
            {
                name: "gcjs1", des: "过程警示字1", type: "int8", ftype: "",
                len: 1,
                alert: [{ level: 1, low: 1, high: 20 },
                    { level: 2, low: 31, high: 50 },
                    { level: 3, low: 61, high: 80 }]
                //1-20 第一层权限 暂定6个报警，1-6
                //31-50 第二层权限，暂定3个报警 31-33
                //61-80 第三层权限，暂定3 个报警 61-63
            },
            {
                name: "gcjs2", des: "过程警示字2", type: "int8", ftype: "", len: 1,
                alert: [{ level: 1, low: 0, high: 12 }]
            },
            { name: "cell", des: "电池电量 0-100", type: "int8", ftype: "", len: 1 },
            { name: "memo", des: "备用字段", type: "int8", ftype: "", len: 1 },
            { name: "value1", des: "过程测量值1-浇筑深度", type: "float", ftype: "bige", len: 4 },
            { name: "value2", des: "过程测量值2-探测孔深", type: "float", ftype: "bige", len: 4 },
            { name: "value3", des: "过程测量值3-预留", type: "float", ftype: "bige", len: 4 }
            ]
        },

        {
            cmd: 0x04 //收到服务器设置指令
        },
        {
            cmd:0x03 //收到服务器控制指令
        }
    ]
};
obj.protocol_sc = {
    cmd: { offset: 1, len: 1 },
    contentlen: { offset: 2, len: 1 },
    bodyfields: [
        {
            cmd: 0x01, //命令字=1 数据长度=2 发送2个byte 8位命令//控制数据包
            // dir: "sc", //server-->client
            field: [
                {
                    name: "controldata0",
                    des: "控制数据-命令字1",
                    type: "int8",
                    ftype: "",
                    len: 1,
                    values: {
                        up:{ value: 1,   des: "手动升" },
                        down:{value: 2,   des: "手动降" },
                        check:{value: 3,   des: "正常检测" },
                        check_top:{ value: 4,   des: "桩顶检测" },
                        sure:{value: 80,  des: "拔管确认" },
                        stop:{value: 101, des: "停机" },
                        reset:{value: 102, des: "复位" }
                        }
                },

                {//备用 
                    name: "controldata1", 
                    des: "控制数据-命令字2", 
                    type: "int8", 
                    ftype: "", 
                    len: 1 
                }
            ]
         },
      {
          cmd: 0x02, //命令字2 设定数据包
          //dir: "sc",//server-->client
          field: [
            { 
                name: "configdata0", 
                des: "设计孔深", 
                type: "float", 
                ftype: "bige", 
                len: 4 
            },
            { 
                name: "configdata1", 
                des: "桩直径", 
                type: "float", 
                ftype: "bige", 
                len: 4 
            },
            {  
                name: "configdata2", 
                des: "承台深度", 
                type: "float", 
                ftype: "bige", 
                len: 4 
            },
            {   
                name: "configdata3", 
                des: "暂时备用", 
                type: "float", 
                ftype: "bige", 
                len: 4 
            }
          ]
      }
    ]
};

var post_option = [
    {
        option: null,
        url: "http://127.0.0.1:9069/sensor_data_public2/8053"
    }
];

function cb(ret, info) {
    if (ret == 500)
        console.log("post error info:", info);
}

function callback(data) {
    var data_1 = {};
    data.field.deviceid = data.deviceid;
    data_1.data = data.field;
    data_1.data_ext = null;
    data_1.alert = [101, 95, 97, 106, 97, 107, 108, 100, 171, 132, 64, 63
    ];
      //pdata = {};
    //Object.assign(pdata, data.field);
    //pdata.deviceid = data.deviceid;
    for (var i = 0; i < post_option.length; i++) {
        //console.log("ddd:",post_option[i].option);
        server_post.post_data( data_1 , post_option[i].option, null, cb);
    }
    //如果远程地址有,则post上去
   // console.log(data);
    return; 
}

server.ProcessTcpServer(obj.protocol_cs,obj.protocol_sc, obj.port, callback);
